import {useUserStore} from "@/store/user";
import {checkPermission} from "@/utils/index.js";
import router from "@/router/index.js";

/**
 * usePermission Hook
 * 自动读取当前路由的 meta.actions
 */
export function usePermission() {
    const route = router.currentRoute.value;
    const userStore = useUserStore();

    // 当前路由的 actions（可能是对象或空）
    const actions = route.meta?.actions || {};
    /**
     * 判断是否有权限
     * @param {string} actionKey - actions 的键，比如 'delete'、'query'
     * @returns {boolean}
     */
    function hasPermission(actionKey) {
        return checkPermission(actionKey, actions, userStore.permissionList);
    }

    return {
        actions,
        hasPermission
    };
}
